<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.png">
  <link rel="mask-icon" href="/images/favicon.png" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" integrity="sha256-Z1K5uhUaJXA7Ll0XrZ/0JhX4lAtZFpT6jkKrEDT0drU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"it-liupp.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.14.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="摘要：MacOS下免密登录的一些注意事项。">
<meta property="og:type" content="article">
<meta property="og:title" content="MacOS Sonoma14.2.1系统SSH免密登录">
<meta property="og:url" content="https://it-liupp.gitee.io/2023/12/25/mac-ssh-autologin/index.html">
<meta property="og:site_name" content="Hello Mr Liu">
<meta property="og:description" content="摘要：MacOS下免密登录的一些注意事项。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://it-liupp.gitee.io/images/mac-ssh-autologin/image-20231225100704655.png">
<meta property="og:image" content="https://it-liupp.gitee.io/images/mac-ssh-autologin/image-20231225100922048.png">
<meta property="og:image" content="https://it-liupp.gitee.io/images/mac-ssh-autologin/image-20231225101350885.png">
<meta property="article:published_time" content="2023-12-25T02:08:18.000Z">
<meta property="article:modified_time" content="2024-01-23T03:36:31.121Z">
<meta property="article:author" content="Mr Liu">
<meta property="article:tag" content="MacOS14">
<meta property="article:tag" content="ssh">
<meta property="article:tag" content="ssh免密登录">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://it-liupp.gitee.io/images/mac-ssh-autologin/image-20231225100704655.png">


<link rel="canonical" href="https://it-liupp.gitee.io/2023/12/25/mac-ssh-autologin/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://it-liupp.gitee.io/2023/12/25/mac-ssh-autologin/","path":"2023/12/25/mac-ssh-autologin/","title":"MacOS Sonoma14.2.1系统SSH免密登录"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>MacOS Sonoma14.2.1系统SSH免密登录 | Hello Mr Liu</title>
  






  <script async defer data-website-id="" src=""></script>

  <script defer data-domain="" src=""></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">Hello Mr Liu</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">临江对月</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E7%8E%AF%E5%A2%83"><span class="nav-number">1.</span> <span class="nav-text">系统环境</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SSH%E5%85%8D%E5%AF%86%E7%99%BB%E5%BD%95"><span class="nav-number">2.</span> <span class="nav-text">SSH免密登录</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%98%E5%9C%A8%E9%97%AE%E9%A2%98%E5%8F%8A%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88"><span class="nav-number">3.</span> <span class="nav-text">存在问题及解决方案</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Mr Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Mr Liu</p>
  <div class="site-description" itemprop="description">和意识先进的人在一起你才能转变意识，和心态积极的人在一起，你才能够积极，靠自己改变很难，一个人拽着自己的头发是很难把自己拎起来的。</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">24</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdnjs.cloudflare.com/ajax/libs/creativecommons-vocabulary/2020.11.3/assets/license_badges/big/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://it-liupp.gitee.io/2023/12/25/mac-ssh-autologin/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Mr Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Hello Mr Liu">
      <meta itemprop="description" content="和意识先进的人在一起你才能转变意识，和心态积极的人在一起，你才能够积极，靠自己改变很难，一个人拽着自己的头发是很难把自己拎起来的。">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="MacOS Sonoma14.2.1系统SSH免密登录 | Hello Mr Liu">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MacOS Sonoma14.2.1系统SSH免密登录
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-12-25 10:08:18" itemprop="dateCreated datePublished" datetime="2023-12-25T10:08:18+08:00">2023-12-25</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-01-23 11:36:31" itemprop="dateModified" datetime="2024-01-23T11:36:31+08:00">2024-01-23</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">操作系统</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/SSH/" itemprop="url" rel="index"><span itemprop="name">SSH</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>758</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>3 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <blockquote>
<p>摘要：MacOS下免密登录的一些注意事项。</p>
</blockquote>
<span id="more"></span>

<h2 id="系统环境"><a href="#系统环境" class="headerlink" title="系统环境"></a>系统环境</h2><blockquote>
<p>操作系统：macOS Sonoma 14.2.1 </p>
</blockquote>
<h2 id="SSH免密登录"><a href="#SSH免密登录" class="headerlink" title="SSH免密登录"></a>SSH免密登录</h2><p>ssh免密登录的原理是在本机生成本机的ssh公钥和私钥，将公钥上传至待连接的主机，本机通过私钥进行ssh连接实现免密登录。</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># ssh密钥生成命令</span>
ssh-keygen

<span class="token comment"># ssh上传(复制)命令</span>
<span class="token comment"># ssh-copy-id格式：ssh-copy-id -i 私钥 远程主机用户名@远程主机IP</span>
<span class="token comment"># ssh-copy-id命令示例</span>
ssh-copy-id -i ~/.ssh/id_liupp root@192.168.8.187

<span class="token comment"># ssh登录</span>
<span class="token comment"># 命令格式： ssh -i 私钥 远程主机用户名@远程主机IP</span>
<span class="token comment"># ssh登录示例：</span>
<span class="token function">ssh</span> -i ~/.ssh/id_liupp root@192.168.8.187<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>关于这两个命令的具体用法可以通过搜索引擎查询，以下为部分参考链接：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/Xixi0864/article/details/134694487">如何设置SSH免密登录，只需两步（小白也能学会）-CSDN博客</a></li>
<li><a target="_blank" rel="noopener" href="https://cloud.tencent.com/developer/article/1720991">Linux 配置SSH免密登录 “ssh-keygen”的基本用法-腾讯云开发者社区-腾讯云 (tencent.com)</a></li>
</ul>
<p>但在Mac上按照说明配置成功后，直接输入：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># ssh免密连接命令</span>
<span class="token function">ssh</span> root@IP<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>回车后仍然提示输入密码，通过搜索引擎查询，得出的结果是mac系统下需要使用“ssh-add”命令将私钥添加至ssh-agent高速缓存：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># ssh-add 命令格式</span>
<span class="token comment"># ssh -add -K 私钥</span>
<span class="token comment"># 示例：</span>
<span class="token function">ssh</span> -add -K ~/.ssh/id_liupp<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>但输入后提示：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">WARNING: The -K and -A flags are deprecated and have been replaced
         by the --apple-use-keychain and --apple-load-keychain
         flags, respectively.  To suppress this warning, <span class="token builtin class-name">set</span> the
         environment variable APPLE_SSH_ADD_BEHAVIOR as described <span class="token keyword">in</span>
         the ssh-add<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> manual page.<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>释义为-K参数对于Mac来说已经过期，需要将“-K”参数换成“–apple-use-keychain”：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># ssh-add 命令格式</span>
<span class="token comment"># ssh -add -K 私钥</span>
<span class="token comment"># 示例：</span>
ssh-add --apple-use-keychain ~/.ssh/id_liupp<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>添加成功后再在终端执行 “ssh root@IP”即可实现免密登录。</p>
<h2 id="存在问题及解决方案"><a href="#存在问题及解决方案" class="headerlink" title="存在问题及解决方案"></a>存在问题及解决方案</h2><p>经过使用发现一个问题，即是在本机重启后，免密登录会失效，经查询得知，ssh-add将私钥添加到高速缓存，但缓存在重启后会失效，需要每次重启后重新添加，在mac下的解决方案是：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 1.在“应用程序”中的找到“自动操作”应用（或CTRL+SPACE）搜索自动操作</span>
<span class="token comment"># 2.打开后默认弹出选择文件对话框，点击“取消”，点击启动栏的自动操作图标，“选取文稿类型”中选择“应用程序”：</span>
<span class="token comment"># 3.左侧“资源库”选择“实用工具”，双击右侧的“运行shell脚本”将ssh-add命令复制至运行脚本脚本中，点击标题栏左侧的“未命名”，起个名字（这里命名为add-ssh-key）并保存至指定位置。</span>
<span class="token comment"># 4.打开“系统设置”,左侧搜索栏搜索“登录项”，在“登录项”的“登录时打开”中将保存的自动执行程序（add-ssh-key）添加进来.</span>
<span class="token comment"># 5.重启后验证，至此配置完毕。</span>
<span class="token comment"># 6.操作步骤图示见下方：</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p><img src="/../images/mac-ssh-autologin/image-20231225100704655.png" alt="image-20231225100704655"></p>
<p><img src="/../images/mac-ssh-autologin/image-20231225100922048.png" alt="image-20231225100922048"></p>
<p><img src="/../images/mac-ssh-autologin/image-20231225101350885.png" alt="image-20231225101350885"></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>☕请作者喝杯咖啡☕</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wechatpay.jpg" alt="Mr Liu 微信">
        <span>微信</span>
      </div>
      <div>
        <img src="/images/alipay.jpg" alt="Mr Liu 支付宝">
        <span>支付宝</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Mr Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="https://it-liupp.gitee.io/2023/12/25/mac-ssh-autologin/" title="MacOS Sonoma14.2.1系统SSH免密登录">https://it-liupp.gitee.io/2023/12/25/mac-ssh-autologin/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

          <div class="post-tags">
              <a href="/tags/MacOS14/" rel="tag"># MacOS14</a>
              <a href="/tags/ssh/" rel="tag"># ssh</a>
              <a href="/tags/ssh%E5%85%8D%E5%AF%86%E7%99%BB%E5%BD%95/" rel="tag"># ssh免密登录</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/12/21/openeuler-install-miniocluster/" rel="prev" title="OpenEuler22.03 SP2 LTS安装Minio2023集群">
                  <i class="fa fa-chevron-left"></i> OpenEuler22.03 SP2 LTS安装Minio2023集群
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/12/29/openeuler-install-pgsql14/" rel="next" title="安装PostgreSQL15.5并配置一主二从">
                  安装PostgreSQL15.5并配置一主二从 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mr Liu</span>
</div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">42k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:32</span>
  </span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  




  





</body>
</html>
